//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <math.h>
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
#include "Unit5.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
        InitArray();
        DrawGraph();
}
//---------------------------------------------------------------------------
void TForm1::Trans(TPoints& p, int m, int n) {
        p.x += m;
        p.y += n;
}
//---------------------------------------------------------------------------
void TForm1::Zoom(TPoints* arr, int len, double xcoef, double ycoef) {
        for (int i=0; i<len; i++) {
                arr[i].x -= center.x;
                arr[i].y -= center.y;
                arr[i].x *= xcoef;
                arr[i].y *= ycoef;
                arr[i].x += center.x;
                arr[i].y += center.y;
        }
}
//---------------------------------------------------------------------------
void TForm1::Turn(TPoints& p, double angl, TPoints axis) {
        TPoints tmp;
        tmp.x = p.x;
        tmp.y = p.y;
        p.x = tmp.x*cos(angl) - p.y*sin(angl) - axis.x*cos(angl) + axis.x + axis.y*sin(angl);
        p.y = tmp.x*sin(angl) + p.y*cos(angl) - axis.x*sin(angl) - axis.y*cos(angl) + axis.y;
}
//---------------------------------------------------------------------------
void TForm1::InitArray() {
        o.x = 0;
        o.y = 0;
        center.x = 0;
        center.y = 0;
        for (int i=0; i<7; i++) {
                apoints[i].x = 0;
                apoints[i].y = -100;
                Turn(apoints[i], (6.2831852*i)/7.0, o);
                bpoints[i].x = 0;
                bpoints[i].y = -40;
                Turn(bpoints[i], (6.2831852*i+3.1415926)/7.0, o);
        }
        for (int i=0; i<360; i++) {
                ellipse[i].x = 100;
                ellipse[i].y = 0;
                Turn(ellipse[i], 6.2831852*i/360, o);
                //if () {}
        }
        Trans(o, 200, 200);
        Trans(center, 200, 200);
        for (int i=0; i<7; i++) {
                Trans(apoints[i], 200, 200);
                Trans(bpoints[i], 200, 200);
        }
        for (int i=0; i<360; i++) {
                Trans(ellipse[i], 200, 200);
        }
}
//---------------------------------------------------------------------------
void TForm1::DrawGraph() {
        TRect img(0,0,Image1->Width, Image1->Height);
        Image1->Canvas->FillRect(img);
        for (int i=1; i<=7; i++) {
                Image1->Canvas->MoveTo(apoints[(i-1)%7].x, apoints[(i-1)%7].y);
                Image1->Canvas->LineTo(bpoints[(i-1)%7].x, bpoints[(i-1)%7].y);
                Image1->Canvas->LineTo(apoints[i%7].x, apoints[i%7].y);
        }
        //PixelFill1(center.x, center.y, 0, 255);

        Image1->Canvas->MoveTo(ellipse[0].x, ellipse[0].y);
        for (int i=1; i<360; i++) {
                Image1->Canvas->LineTo(ellipse[i].x, ellipse[i].y);
        }
        Image1->Canvas->LineTo(ellipse[0].x, ellipse[0].y);

        Image1->Canvas->MoveTo(bpoints[0].x, bpoints[0].y);
        Image1->Canvas->LineTo(bpoints[4].x, bpoints[4].y);
        Image1->Canvas->MoveTo(bpoints[6].x, bpoints[6].y);
        Image1->Canvas->LineTo(bpoints[2].x, bpoints[2].y);

        Image1->Canvas->MoveTo(0, o.y);
        Image1->Canvas->LineTo(Image1->Width, o.y);
        Image1->Canvas->MoveTo(o.x, 0);
        Image1->Canvas->LineTo(o.x, Image1->Height);
        Image1->Canvas->MoveTo(0, 400);
        Image1->Canvas->LineTo(400, 0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
        int step = StrToInt(Edit1->Text);
        for (int i=0; i<7; i++) {
                apoints[i].y -= step;
                bpoints[i].y -= step;
        }
        for (int j=0; j<360; j++) {
                ellipse[j].y -= step;
        }
        center.y -= step;
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{
        int step = StrToInt(Edit1->Text);
        for (int i=0; i<7; i++) {
                apoints[i].x -= step;
                bpoints[i].x -= step;
        }
        for (int j=0; j<360; j++) {
                ellipse[j].x -= step;
        }
        center.x -= step;
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton5Click(TObject *Sender)
{
        int step = StrToInt(Edit1->Text);
        for (int i=0; i<7; i++) {
                apoints[i].x += step;
                bpoints[i].x += step;
        }
        for (int j=0; j<360; j++) {
                ellipse[j].x += step;
        }
        center.x += step;
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
        int step = StrToInt(Edit1->Text);
        for (int i=0; i<7; i++) {
                apoints[i].y += step;
                bpoints[i].y += step;
        }
        for (int j=0; j<360; j++) {
                ellipse[j].y += step;
        }
        center.y += step;
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton6Click(TObject *Sender)
{
        double coef = StrToFloat(Edit2->Text)/100.0;
        Zoom(apoints, 7, coef, 1);
        Zoom(bpoints, 7, coef, 1);
        Zoom(ellipse, 360, coef, 1);
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton7Click(TObject *Sender)
{
        double coef = StrToFloat(Edit3->Text)/100.0;
        Zoom(apoints, 7, 1, coef);
        Zoom(bpoints, 7, 1, coef);
        Zoom(ellipse, 360, 1, coef);
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton8Click(TObject *Sender)
{
        double angl = StrToFloat(Edit4->Text)*M_PI/180;
        TPoints axis;
        axis.x = StrToInt(Edit5->Text);
        axis.y = StrToInt(Edit6->Text);
        Trans(axis, 200, 200);
        for (int i=0; i<7; i++) {
                Turn(apoints[i], angl, axis);
                Turn(bpoints[i], angl, axis);
        }
        for (int j=0; j<360; j++) {
                Turn(ellipse[j], angl, axis);
        }
        Turn(center, angl, axis);
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
        for (int i=0; i<7; i++) {
                apoints[i].y = 400-apoints[i].y;
                bpoints[i].y = 400-bpoints[i].y;
        }
        for (int j=0; j<360; j++) {
                ellipse[j].y = 400-ellipse[j].y;
        }
        center.y = 400 - center.y;
        DrawGraph();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton9Click(TObject *Sender)
{
        for (int i=0; i<7; i++) {
                apoints[i].x = 400-apoints[i].x;
                bpoints[i].x = 400-bpoints[i].x;
        }
        for (int j=0; j<360; j++) {
                ellipse[j].x = 400-ellipse[j].x;
        }
        center.x = 400 - center.x;
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton10Click(TObject *Sender)
{
        int tmp;
        for (int i=0; i<7; i++) {
                tmp = apoints[i].x;
                apoints[i].x = 400 - apoints[i].y;
                apoints[i].y = 400 - tmp;
                tmp = bpoints[i].x;
                bpoints[i].x = 400 - bpoints[i].y;
                bpoints[i].y = 400 - tmp;
        }
        for (int j=0; j<360; j++) {
                tmp = ellipse[j].x;
                ellipse[j].x = 400 - ellipse[j].y;
                ellipse[j].y = 400 - tmp;
        }
        tmp = center.x;
        center.x = 400 - center.y;
        center.y = 400 - tmp;
        DrawGraph();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::SpeedButton11Click(TObject *Sender)
{
        InitArray();
        DrawGraph();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Reset1Click(TObject *Sender)
{
        Form1->SpeedButton11Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Close1Click(TObject *Sender)
{
        Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Moveleft1Click(TObject *Sender)
{
        Form2->Caption = "Move left";
        if (Form2->ShowModal() == mrOk) {
                Form1->SpeedButton4Click(Sender);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Moveright1Click(TObject *Sender)
{
        Form2->Caption = "Move right";
        if (Form2->ShowModal() == mrOk) {
                Form1->SpeedButton5Click(Sender);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Moveup1Click(TObject *Sender)
{
        Form2->Caption = "Move up";
        if (Form2->ShowModal() == mrOk) {
                Form1->SpeedButton2Click(Sender);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Movedown1Click(TObject *Sender)
{
        Form2->Caption = "Move down";
        if (Form2->ShowModal() == mrOk) {
                Form1->SpeedButton3Click(Sender);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ZoomX1Click(TObject *Sender)
{
        Form3->Caption = "X-axis zoom";
        if (Form3->ShowModal() == mrOk) {
                Form1->SpeedButton6Click(Sender);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ZoomY1Click(TObject *Sender)
{
        Form3->Caption = "Y-axis zoom";
        if (Form3->ShowModal() == mrOk) {
                Form1->SpeedButton7Click(Sender);
        }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Turn1Click(TObject *Sender)
{
        Form4->Caption = "Turn graph";
        if (Form4->ShowModal() == mrOk) {
                Form1->SpeedButton8Click(Sender);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Xsymmetry1Click(TObject *Sender)
{
        Form1->SpeedButton1Click(Sender);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ysymmetry1Click(TObject *Sender)
{
        Form1->SpeedButton9Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::XYsymmetry1Click(TObject *Sender)
{
        Form1->SpeedButton10Click(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::About1Click(TObject *Sender)
{
        Form5->ShowModal();
}
//---------------------------------------------------------------------------
/*
void TForm1::PixelFill1(int x, int y, int border_col, int col) {
		int c = Image1->Canvas->Pixels[x][y];
		if (c!=border_col && c!=col && x<Image1->ClientWidth && y<Image1->ClientHeight && x>=0 && y>=0) {
				Image1->Canvas->Pixels[x][y] = col;
				PixelFill1(x-1, y, border_col, col);
				PixelFill1(x+1, y, border_col, col);
				PixelFill1(x, y-1, border_col, col);
				PixelFill1(x, y+1, border_col, col);
		}
}*/
